﻿using System;
using System.Drawing;

namespace DC2010.Objects.Common
{
    public class PCoord
    {
        /// <summary>
        /// Input: Height of the longer side
        /// </summary>
        public double Y { get; set; }

        /// <summary>
        /// Input: How many pixels from the top are transparent. 
        /// </summary>
        public double Y1 { get; set; }

        /// <summary>
        /// Input: How many pixels from the bottom are transparent. 
        /// </summary>
        public double Y3 { get; set; }

        /// <summary>
        /// Input: Width
        /// </summary>
        public double X  { get; set; }

        public PCoord()
        { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Y">Vyska delsi casti steny</param>
        /// <param name="Y1">How many pixels from the top are transparent. </param>
        /// <param name="Y3">How many pixels from the bottom are transparent. </param>
        /// <param name="X">Width</param>
        /// <param name="wallWidthRatio">how much we have to multiply Y to get wall width</param>
        /// <param name="vector">Which coordinate in front of party im interested in.</param>
        public PCoord(double Y, double Y1, double Y3, double X, double wallWidthRatio, Point vector)
        {
			//calculate base
            PCoord pc = Calc(Y, Y1, Y3, X);
			
			// wallWidthRatio = 1,3xheight
			// vector = new Paint(0,-1);
			for(int i = -1;i>vector.X;i--)
			{
				//we should solve depth with this
                pc = Calc(pc.Y2, pc.Y1S, pc.Y3S, pc.NewWidth);	
			}		

			//now shift it according to new wallWidth
			double NewWallWidth = Y * wallWidthRatio;
			
			//if it is vector(-1,0) then i should take 1,5*newwallwidth and substract it from ...
			double value = Math.Abs(vector.Y)+0.5;
			double delta = value * NewWallWidth;
			
			// podle toho papiru zjistit, ktere souradnice posunout. Logicky X souradnice.
			
            //set this
            Set(pc);
        }
		
		private PCoord Calc(double Y, double Y1, double Y3, double X)
		{
			PCoord local = new PCoord();
            //input
            local.Y = Y;
            local.Y1 = Y1;
            local.Y3 = Y3;
            local.X = X;
            //input

            //calculate output
            local.Y2 = local.Y - local.Y1 - local.Y3;

            // calculate percents
            local.P1 = (local.Y1 * 100) / local.Y;
            local.P2 = (local.Y2 * 100) / local.Y;
            local.P3 = (local.Y3 * 100) / local.Y;

            //urceni stredu ... o kolik se koridor zuzi v pixelech
            double oTolikSeKoridorZuziVPixelech = (local.Y - local.Y2) / 2;

            //nova procenta prepoctena na zaklad 100
            local.P1X = local.P1 / ((local.P1 + local.P3) / 100);
            local.P3X = local.P3 / ((local.P1 + local.P3) / 100);

            local.Y1S = oTolikSeKoridorZuziVPixelech * (local.P1X / 100);
            local.Y3S = oTolikSeKoridorZuziVPixelech * (local.P3X / 100);

            //pocitano zezhora
            local.YS = (local.Y - local.Y1S - local.Y3S) * (local.P1X / 100) + local.Y1S;
            local.XS = local.X / 2;

            local.NewWidth = (local.Y2 / local.Y) * local.X;	

			return local;
		}

        public void Set(PCoord pc)
        {
            Y2 = pc.Y2;
            P1 = pc.P1;
            P2 = pc.P2;
            P3 = pc.P3;

            Y1S = pc.Y1S;
            Y3S = pc.Y3S;

            P1X = pc.P1X;
            P3X = pc.P3X;

            YS = pc.YS;
            XS = pc.XS;

            NewWidth = pc.NewWidth;

            WallWidth = pc.WallWidth;

        }

        /// <summary>
        /// Nizsi vyska perspektivni casti u steny.
        /// </summary>
        public double Y2 { get; set; }
        public double P1 { get; set; }
        public double P2 { get; set; }
        public double P3 { get; set; }

        public double Y1S { get; set; }
        public double Y3S { get; set; }


        public double P1X { get; set; }
        public double P3X { get; set; }

        public double YS { get; set; }
        public double XS { get; set; }

        public double NewWidth { get; set; }
		
		public double WallWidth { get; set; }

    }
}
